package com.example.yls.demoa;

import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class SearchActivity extends AppCompatActivity {

    private EditText edtNo;
    private Button btnQuery;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_search);

        initViews();

        btnQuery.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                MyDataBaseHelper dataBaseHelper = new MyDataBaseHelper(SearchActivity.this);
                SQLiteDatabase db = dataBaseHelper.getReadableDatabase();
                String key = edtNo.getText().toString();

                String where = "no="+key;
                Cursor cursor = db.query("student",null,where,null,null,null,null);

                cursor.moveToFirst();
                String name = cursor.getString(cursor.getColumnIndex("name"));
                String birth = cursor.getString(cursor.getColumnIndex("birth"));
                String phone = cursor.getString(cursor.getColumnIndex("phone"));
                cursor.close();

                Intent intent = new Intent(SearchActivity.this,StudentActivity.class);
                intent.putExtra("NO",key);
                intent.putExtra("NAME",name);
                intent.putExtra("BIRTH",birth);
                intent.putExtra("PHONE",phone);
                startActivity(intent);


            }
        });
    }

    private void initViews() {
        edtNo = findViewById(R.id.edt_no);
        btnQuery = findViewById(R.id.btn_query);
    }
}
